package com.example.administrator.hqq.View;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2019/3/12 18:27
 * @remark:
 */
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class KeyboardLayout extends RelativeLayout { private static final String TAG = KeyboardLayout.class.getSimpleName();
    public static final byte KEYBOARD_STATE_SHOW = -3;
    public static final byte KEYBOARD_STATE_HIDE = -2;
    public static final byte KEYBOARD_STATE_INIT = -1;
    private boolean mHasInit;
    private boolean mHasKeybord;
    private int mHeight;
    private onKeyboaddsChangeListener mListener;

    public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);
    } public KeyboardLayout(Context context, AttributeSet attrs) { super(context, attrs);
    } public KeyboardLayout(Context context) { super(context);
    } /**
     * set keyboard state listener
     */
    public void setOnkbdStateListener(onKeyboaddsChangeListener listener)
    {
        mListener = listener;

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b);
        if (!mHasInit) { mHasInit = true;
            mHeight = b;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
            }
        } else {
            mHeight = mHeight < b ? b : mHeight;
        }
        Log.e(TAG, "onLayout: mHeight++++"+mHeight+" b++++"+b+"mHasInit++++"+mHasInit );
        if (mHasInit&&mHeight-b>300)
        {
            mHasKeybord = true;
        }
        if (mHasInit && mHeight-b > 300) {
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
            }
            Log.w(TAG, "show keyboard.......");
        }
        if (mHasInit && mHasKeybord && mHeight-b<300) {
            mHasKeybord = false;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
            }
            Log.w(TAG, "hide keyboard.......");
        }
        if (mHasInit&& mHeight-b<300)
        {
            mHasKeybord = false;
        }
    }
    public interface onKeyboaddsChangeListener{
        public void onKeyBoardStateChange(int state);
    }
}